//
//  CustomPicker.m
//  Sloth
//
//  Created by 焱 孙 on 13-5-2.
//
//

#import "CustomPicker.h"

@implementation CustomPicker
@synthesize pickerView;
@synthesize btnDoneItem;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andDelegate:(id<CustomPickerDelegate,UIPickerViewDataSource,UIPickerViewDelegate>)tempDelegate
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        self.delegate = tempDelegate;
        self.backgroundColor = [UIColor whiteColor];
        
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, 216)];
        self.pickerView.delegate = self.delegate;
        self.pickerView.dataSource = self.delegate;
        [self addSubview: pickerView];
        
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, kScreenWidth, 44)];
        toolbar.barStyle = UIBarStyleBlack;
        toolbar.translucent = YES;
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"完成" style: UIBarButtonItemStyleBordered target: self action: @selector(doneClick)];
        doneButton.style = UIBarButtonItemStyleDone;
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style: UIBarButtonItemStyleBordered target: self action: @selector(cancelClick)];
        cancelButton.style = UIBarButtonItemStyleDone;
        UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        toolbar.items = [NSArray arrayWithObjects:cancelButton,flexibleSpace,doneButton, nil];
        [self addSubview:toolbar];
    }
    return self;
}

-(void)doneClick
{
    [delegate donePickerButtonClick:self];
}

-(void)cancelClick
{
    [self.delegate cancelPickerButtonClick:self];
}


-(NSInteger)getSelectedRowNum
{
    return [self.pickerView selectedRowInComponent:0];
}

@end
